<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2017/4/16
 * Time: 14:34
 */

namespace app\home\controller\user;


class Article extends Common
{
    private $mod;
    private $_cate_mod;
    public function _initialize()
    {
        parent::_initialize();
        $this->mod = model('article');
        $this->_cate_mod = model('article_cate');
        $this->_name = 'Article';
    }
    public function index(){
        $where['user_id'] = $this->userInfo['id'];
        $lists = $this->mod->where($where)->field('id,title,cate_id,hits,status,create_time')->order('id desc')->paginate(10);
        $this->assign('lists',$lists);
        $this->assign('pages',$lists->render());
        return $this->fetch();
    }
    //编辑 前置事件
    public function editDo(){
        \app\common\model\Article::event('before_update',function($obj){
            //自动提取摘要
            if($obj->description == '' && isset($obj->info)) {
                $content = stripslashes($obj->info);
                $obj->description = msubstr(str_replace(array("'","\r\n","\t",'[page]','[/page]','&ldquo;','&rdquo;','&nbsp;'), '', strip_tags($content)),0,200);
                $obj->description = addslashes($obj->description);
            }
            $obj->cate_alias = $this->_cate_mod->getCateAlias($obj->cate_id);
            if(isset($obj->status) && $obj->status==1){
                $obj->status = cache('user')['article'];
            }
        });
        parent::editDo();
    }
    //添加前置事件
    public function addDo(){
        \app\common\model\Article::event('before_insert',function($obj){
            //自动提取摘要
            if($obj->description == '' && isset($obj->info)) {
                $content = stripslashes($obj->info);
                $obj->description = msubstr(str_replace(array("'","\r\n","\t",'[page]','[/page]','&ldquo;','&rdquo;','&nbsp;'), '', strip_tags($content)),0,200);
                $obj->description = addslashes($obj->description);
            }
            $obj->user_id    = $this->userInfo['id'];
            $obj->editor     = $this->userInfo['nickname'];
            $obj->cate_alias = $this->_cate_mod->getCateAlias($obj->cate_id);
            if($obj->status==1){
                $obj->status = cache('user')['article'];
            }
        });
        parent::addDo();
    }
}